package Sort;

import java.util.Arrays;

public class CountSort {
    public void operation(int[] arr) {
        int min = arr[0];
        int max = arr[0];

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
            if (arr[i] > max) {
                max = arr[i];
            }
        }

        int[] countArr = new int[max - min + 1];

        for (int i = 0; i < arr.length; i++) {
            countArr[arr[i] - min]++;
        }

        int j = 0;
        for (int i = 0; i < countArr.length; i++) {
            int num = countArr[i];
            while (num != 0) {
                arr[j++] = i + min;
                num--;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {9,9,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1,1};

        CountSort countSort = new CountSort();
        countSort.operation(arr);
        System.out.println(Arrays.toString(arr));
    }
}
